<?php

/**********************************************************************
Copyright (C) FrontAccounting, LLC.
Released under the terms of the GNU General Public License, GPL, 
as published by the Free Software Foundation, either version 3 
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$path_to_root = "../..";

include_once ($path_to_root . "/includes/db_pager.inc");
include_once ($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/sales/includes/sales_ui.inc");
include_once ($path_to_root . "/reporting/includes/reporting.inc");
include_once ($path_to_root . "/admin/db/fiscalyears_db.inc");

$page_security = 'SA_SALESTRANSVIEW';

set_page_security(@$_POST['order_view_mode'], array('OutstandingOnly' =>
        'SA_SALESDELIVERY', 'InvoiceTemplates' => 'SA_SALESINVOICE'), array('OutstandingOnly' =>
        'SA_SALESDELIVERY', 'InvoiceTemplates' => 'SA_SALESINVOICE'));
$trans_type = ST_SALESORDER;

if ($trans_type == ST_SALESORDER)
{
    if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
    {
        $_POST['order_view_mode'] = 'OutstandingOnly';
        $_SESSION['page_title'] = _($help_context = "Search Outstanding Sales Orders");
    } elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
    {
        $_POST['order_view_mode'] = 'InvoiceTemplates';
        $_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
    } elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
    {
        $_POST['order_view_mode'] = 'DeliveryTemplates';
        $_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
    } elseif (!isset($_POST['order_view_mode']))
    {
        $_POST['order_view_mode'] = false;
        $_SESSION['page_title'] = _($help_context = "Daftar Faktur Penjualan");
    }
}

if (!@$_GET['popup'])
{
    $js = "";
    if ($use_popup_windows)
        $js .= get_js_open_window(900, 600);
    if ($use_date_picker)
        $js .= get_js_date_picker();
    page($_SESSION['page_title'], false, false, "", $js);
}

if (isset($_GET['void']))
{
    $row = $_GET['void'];
    //display_error($row);
    void_sales_orders($row);
    //display_error($row);
    meta_forward($_SERVER['PHP_SELF'], 'type=30');
}


if (isset($_GET['selected_customer']))
{
    $selected_customer = $_GET['selected_customer'];
} elseif (isset($_POST['selected_customer']))
{
    $selected_customer = $_POST['selected_customer'];
} else
    $selected_customer = -1;

//---------------------------------------------------------------------------------------------

if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
    ($_POST['SelectStockFromList'] != ALL_TEXT))
{
    $selected_stock_item = $_POST['SelectStockFromList'];
} else
{
    unset($selected_stock_item);
}
//---------------------------------------------------------------------------------------------
//	Query format functions
//
function check_overdue($row)
{
    global $trans_type;
    if ($trans_type == ST_SALESQUOTE)
        return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
    else
        return ($row['type'] == 0 && date1_greater_date2(Today(), sql2date($row['ord_date'])) &&
            ($row['TotDelivered'] < $row['TotQuantity']));
}

function view_link($dummy, $order_no)
{
    global $trans_type;
    return get_customer_trans_view_str($trans_type, $order_no);
}

function prt_link($row)
{
    global $trans_type;
    if (is_faktur_lunas($row['order_no']))
        //return print_document_link($row['order_no'], _("Print"), true, ST_SALESINVOICE, ICON_PRINT);

        return print_document_link($row['order_no'], _("Print"), true, ST_FAKTURTUNAI,
            ICON_PRINT);
    else //return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);

        return print_document_link($row['order_no'], _("Print"), true, ST_FAKTURTUNAI,
            ICON_PRINT);
}

function prt_details_link($row)
{
    global $trans_type;
    return print_document_link($row['order_no'], _("Print Sales Order Details"), true,
        ST_SALESORDERDETIL, ICON_PRINT);
}

function edit_link($row)
{
    if (@$_GET['popup'])
        return '';
    global $trans_type;
    $modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" :
        "ModifyQuotationNumber");
    //return $row;
    if(is_faktur_lunas($row['order_no']) && is_order_finish_all($row['order_no']))
        return '';
        
    return pager_link(_("Edit"), "/sales/sales_order_entry.php?$modify=" . $row['order_no'],
        ICON_EDIT);   
    
}

function void_link($row)
{
    if (@$_GET['popup'])
        return '';
    global $trans_type;
       // var_dump($row);
    if(is_faktur_lunas($row['order_no']) && is_order_finish_all($row['order_no']))
        return '';
        
    if(!is_date_in_fiscalyears(sql2date($row["ord_date"])))
        return '';
    
    return pager_link("Void", "/sales/inquiry/sales_orders_view.php?void=" . $row['order_no'],
        ICON_DELETE);
        
}

function lunas_link($row)
{
    if (@$_GET['popup'])
        return '';
    global $trans_type;
    
    if (is_faktur_lunas($row['order_no']))
        return '';
    return pager_link('Pembayaran', "/sales/cust_pay.php?order_no=" .
        $row['order_no'], ICON_MONEY);
}

function status_link($row)
{
    if (@$_GET['popup'])
        return '';
    global $trans_type;
    
    $status = '';
    
    if (is_faktur_lunas($row['order_no']))
        $status .= 'LUNAS';
    else
        $status .= 'BELUM LUNAS';
        
    $status .= " - ";
    
    if (is_order_finish_all($row['order_no']))
        $status .= 'SELESAI';
    else        
        $status .= 'PROSES';
    return $status;                   
    //$modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" :
//        "ModifyQuotationNumber");
//    return pager_link(_("Pelunasan"), "/sales/sales_order_finish.php?NewInvoice=0&$modify=" .
//        $row['order_no'], ICON_MONEY);
}

function detil_link($row)
{
    if (@$_GET['popup'])
        return '';
    global $trans_type;
    //$modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" : "ModifyQuotationNumber");
    if (is_faktur_lunas($row['order_no']))
        return '';
    return pager_link(_("Detil"), "/sales/detail_pesanan.php?order_no=" . $row['order_no'],
        ICON_DOC);
}

function dispatch_link($row)
{
    global $trans_type;
    if ($trans_type == ST_SALESORDER)
        return pager_link(_("Dispatch"), "/sales/customer_delivery.php?OrderNumber=" . $row['order_no'],
            ICON_DOC);
    else
        return pager_link(_("Sales Order"), "/sales/sales_order_entry.php?OrderNumber=" .
            $row['order_no'], ICON_DOC);
}

function invoice_link($row)
{
    global $trans_type;
    if ($trans_type == ST_SALESORDER)
        return pager_link(_("Invoice"), "/sales/sales_order_entry.php?NewInvoice=" . $row["order_no"],
            ICON_DOC);
    else
        return '';
}

function delivery_link($row)
{
    return pager_link(_("Delivery"), "/sales/sales_order_entry.php?NewDelivery=" . $row['order_no'],
        ICON_DOC);
}

function order_link($row)
{
    return pager_link(_("Sales Order"),
        "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" . $row['order_no'],
        ICON_DOC);
}

function tmpl_checkbox($row)
{
    global $trans_type;
    if ($trans_type == ST_SALESQUOTE)
        return '';
    if (@$_GET['popup'])
        return '';
    $name = "chgtpl" . $row['order_no'];
    $value = $row['type'] ? 1 : 0;

    // save also in hidden field for testing during 'Update'

    return checkbox(null, $name, $value, true, _('Set this order as a template for direct deliveries/invoices')) .
        hidden('last[' . $row['order_no'] . ']', $value, false);
}
//---------------------------------------------------------------------------------------------
// Update db record if respective checkbox value has changed.
//
function change_tpl_flag($id)
{
    global $Ajax;

    $sql = "UPDATE " . TB_PREF . "sales_orders SET type = !type WHERE order_no=$id";

    db_query($sql, "Can't change sales order type");
    $Ajax->activate('orders_tbl');
}

$id = find_submit('_chgtpl');
if ($id != -1)
    change_tpl_flag($id);

if (isset($_POST['Update']) && isset($_POST['last']))
{
    foreach ($_POST['last'] as $id => $value)
        if ($value != check_value('chgtpl' . $id))
            change_tpl_flag($id);
}

$show_dates = !in_array($_POST['order_view_mode'], array(
    'OutstandingOnly',
    'InvoiceTemplates',
    'DeliveryTemplates'));
//---------------------------------------------------------------------------------------------
//	Order range form
//
if (get_post('_OrderNumber_changed') || get_post('_OrderReference_changed'))
    // enable/disable selection controls
{
    $disable = get_post('OrderNumber') !== '' || get_post('OrderReference') !== '';

    if ($show_dates)
    {
        $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
        $Ajax->addDisable(true, 'OrdersToDate', $disable);
    }

    $Ajax->activate('orders_tbl');
}

if (!@$_GET['popup'])
    start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();
ref_cells(_("#:"), 'OrderNumber', '', null, '', true);
ref_cells(_("Ref"), 'OrderReference', '', null, '', true);
if ($show_dates)
{
    date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
    date_cells(_("to:"), 'OrdersToDate', '', null, 1);
}
//var_dump($_POST);
$_POST['StockLocation'] = '';
//locations_list_cells(_("Location:"), 'StockLocation', null, true);
//hidden('StockLocation',null);

if ($show_dates)
{
    end_row();
    end_table();

    start_table(TABLESTYLE_NOBORDER);
    start_row();
}
//stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
stock_barang_pesanan_items_list_cells("Item:", 'SelectStockFromList', null, true);
if (!@$_GET['popup'])
    customer_list_cells(_("Select a customer: "), 'customer_id', null, true);

if (!@$_GET['popup'])
    status_list_cells("Status Lunas:", 'status', null, true);

if ($trans_type == ST_SALESQUOTE)
    check_cells(_("Show All:"), 'show_all');

submit_cells('SearchOrders', _("Search"), '', _('Select documents'), 'default');
hidden('order_view_mode', $_POST['order_view_mode']);
hidden('type', $trans_type);

end_row();

end_table(1);
//---------------------------------------------------------------------------------------------
//	Orders inquiry table
//
$sql = get_sql_for_sales_orders_view2($selected_customer, $trans_type, $_POST['OrderNumber'],
    $_POST['order_view_mode'], @$selected_stock_item, @$_POST['OrdersAfterDate'], @
    $_POST['OrdersToDate'], @$_POST['OrderReference'], $_POST['StockLocation'], $_POST['customer_id'],
    $_POST['status']);

if ($trans_type == ST_SALESORDER)
    $cols = array(
        _("Order #"),
        "No. Faktur",
        "Kategori",
        "Nama",
        // 		_("Cust Order Ref"),
        _("Order Date") => 'date',
        "Tanggal Dibutuhkan" => array('type' => 'date', 'ord' => ''),
        //_("Delivery To"),
        _("Order Total") => array('type' => 'amount', 'ord' => ''),
        'Type' => 'skip' // 		_("Currency") => array('align'=>'center')
            );
// else
// 	$cols = array(
// 		_("Quote #") => array('fun'=>'view_link'),
// 		_("Ref"),
// 		_("Customer"),
// 		_("Branch"),
// 		_("Cust Order Ref"),
// 		_("Quote Date") => 'date',
// 		_("Valid until") =>array('type'=>'date', 'ord'=>''),
// 		_("Delivery To"),
// 		_("Quote Total") => array('type'=>'amount', 'ord'=>''),
// 		'Type' => 'skip',
// 		_("Currency") => array('align'=>'center')
// 	);
if ($_POST['order_view_mode'] == 'OutstandingOnly')
{
    //array_substitute($cols, 3, 1, _("Cust Order Ref"));
    array_append($cols, array(array('insert' => true, 'fun' => 'dispatch_link'),
            array('insert' => true, 'fun' => 'edit_link')));

} elseif ($_POST['order_view_mode'] == 'InvoiceTemplates')
{
    array_substitute($cols, 3, 1, _("Description"));
    array_append($cols, array(array('insert' => true, 'fun' => 'invoice_link')));

} else
    if ($_POST['order_view_mode'] == 'DeliveryTemplates')
    {
        array_substitute($cols, 3, 1, _("Description"));
        array_append($cols, array(array('insert' => true, 'fun' => 'delivery_link')));

    } elseif ($trans_type == ST_SALESQUOTE)
    {
        array_append($cols, array(
            array('insert' => true, 'fun' => 'edit_link'),
            array('insert' => true, 'fun' => 'order_link'),
            array('insert' => true, 'fun' => 'prt_link')));
    } elseif ($trans_type == ST_SALESORDER)
    {
        array_append($cols, array(
            //array('insert' => true, 'fun' => 'tmpl_checkbox'),
            _("Lihat") => array('insert' => true, 'fun' => 'edit_link'),
            // 					_("Detil") => array('insert'=>true, 'fun'=>'detil_link'),
            "Bayar" => array('insert' => true, 'fun' => 'lunas_link'),
            "Status" => array('insert' => true, 'fun' => 'status_link'),
            "Void" => array('insert' => true, 'fun' => 'void_link'),
            //array('insert' => true, 'fun' => 'prt_link'),
            
            //_("NP") => array('insert' => true, 'fun' => 'prt_details_link')
            ));
    }
;


$table = &new_db_pager('orders_tbl', $sql, $cols);
$table->page_len = 20;
$table->set_marker('check_overdue', _("Marked items are overdue."));

$table->width = "90%";

display_db_pager($table);

echo "<span style='float:left;margin-left:5%;font-weight:bold'>Status</span></br>";
echo "<span style='float:left;margin-left:5%;'>BELUM LUNAS - PROSES : Pembayaran belum lunas dan produksi masih dalam proses</span></br>";
echo "<span style='float:left;margin-left:5%;'>LUNAS - PROSES : Pembayaran lunas dan produksi masih dalam proses</span></br>";
echo "<span style='float:left;margin-left:5%;'>BELUM LUNAS - SELESAI : Pembayaran belum lunas dan produksi sudah selesai</span></br>";
echo "<span style='float:left;margin-left:5%;'>LUNAS - SELESAI : Pembayaran lunas dan produksi sudah selesai</span></br>";
//echo "<span width=\"90%\">Status</span>";
br();
submit_center('Update', _("Update"), true, '', null);

if (!@$_GET['popup'])
{
    end_form();
    end_page();
}

?>